package edu.neu.swc.cloud.service.impl;

import edu.neu.swc.cloud.dao.AdminDao;
import edu.neu.swc.cloud.dao.FactoryDao;
import edu.neu.swc.cloud.domain.Admin;
import edu.neu.swc.cloud.domain.Factory;
import edu.neu.swc.cloud.service.AdminService;
import edu.neu.swc.cloud.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminDao adminDao;

    @Autowired
    FactoryDao factoryDao;

    @Override
    public List<Admin> findAll() {
        List<Admin> admins = adminDao.findAll();
        return admins;
    }

    @Override
    public int addAdmin(Admin admin) {
        Admin target = adminDao.findAdminByUsername(admin.getAd_username());
        if(target == null){
            try {
                String password_MD5 = Md5Util.encodeByMd5(admin.getAd_password());
                admin.setAd_password(password_MD5);
                adminDao.addAdmin(admin);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public Admin findAdminById(int ad_id) {
        return adminDao.findAdminById(ad_id);
    }

    @Override
    public void updateAdmin(Admin admin) {
        adminDao.updateAdmin(admin);
    }

    @Override
    public void deleteAdmin(int admin_id) {
        adminDao.deleteAdminById(admin_id);
    }

    @Override
    public List<Admin> findAdminByCon(Admin admin) {
        return adminDao.findAdminByCon(admin);
    }

    @Override
    public Admin logon(String ad_username, String ad_password) {
        Admin flag;
        String password_MD5 = ad_password;
        try {
            password_MD5 = Md5Util.encodeByMd5(ad_password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Admin> temp_user = adminDao.findByUsernameAndPassword(ad_username, password_MD5);
        if(temp_user.size() == 0){
            flag = null;//登陆失败
        }else{
            Admin user = temp_user.get(0);
            flag = user;
        }
        return flag;
    }

    @Override
    public Factory ownFactory(Admin will) {
        Factory factory = factoryDao.findFactoryBySpon(will.getAd_id());
        return factory;
    }

    @Override
    public int register(Admin admin) {
        Admin target = adminDao.findAdminByUsername(admin.getAd_username());
        if(target == null){
            //此时人员信息可以注册，但是仍然需要判断
            if(admin.getAd_type() == 2){
                //注册云工厂管理员
                Factory fa = factoryDao.findFactoryByName(admin.getFa_name());
                if(fa == null){
                    //此时才允许添加信息
                    //1,添加用户信息
                    String password_MD5 = null;
                    try {
                        password_MD5 = Md5Util.encodeByMd5(admin.getAd_password());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    admin.setAd_password(password_MD5);
                    adminDao.addAdmin(admin);
                    //2,封装工厂类
                    Factory factory = new Factory();
                    factory.setFa_name(admin.getFa_name());
                    factory.setFa_desc(admin.getFa_desc());
                    factory.setFa_spon(admin.getAd_real_name());
                    factory.setFa_conn(admin.getAd_tel());
                    //3,添加工厂信息
                    factoryDao.addFactory(factory);
                    //4,获取新增工厂和新增用户的ID
                    Factory insert_fac = factoryDao.findFactoryByName(factory.getFa_name());
                    Admin insert_adm = adminDao.findAdminByUsername(admin.getAd_username());
                    //5,添加联系集合
                    factoryDao.addFacRel(insert_adm.getAd_id(),insert_fac.getFa_id());
                    //6,返回状态码，正常注册
                    return 1;
                }
            }
            if(admin.getAd_type() == 3){
                //经销商注册信息，直接添加即可
                String password_MD5 = null;
                try {
                    password_MD5 = Md5Util.encodeByMd5(admin.getAd_password());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                admin.setAd_password(password_MD5);
                adminDao.addAdmin(admin);
                return 1;
            }
        }
        //所有其他状况都为异常注册
        return 0;
    }

    @Override
    public int getAdminCounts(String s) {
        return adminDao.getAdminCounts(s);
    }

    @Override
    public List<Admin> findAdminByQuery(String s, int pageStart, int pageSize) {
        return adminDao.findAdminByQuery(s,pageStart,pageSize);
    }
}
